package cn.aishua.ui.test;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

import cn.aishua.location.Locator;
import cn.aishua.location.Locator.Callback;
import cn.aishua.util.Constant;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.MyLocationOverlay;
import cn.aishua.R;

public class MapTestActivity
    extends Activity
{

    MapView mMapView = null;
    private MapController mMapController = null;
    private BMapManager mBMapManager = null;

    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        Log.d( "test", "onCreate" );
        super.onCreate( savedInstanceState );
        initMapManager();
        setContentView( R.layout.map_test );

        mMapView = (MapView) findViewById( R.id.bmapView );

        mBMapManager.start();
        mMapView.setBuiltInZoomControls( true );
        mMapController = mMapView.getController();
        mMapView.setLongClickable( false );
        mMapController.setZoom( 15 );

        mMapView.regMapViewListener( mBMapManager, null );

        mMapView.setOnClickListener( new OnClickListener()
        {
            public void onClick( View v )
            {
                //				Locator.getInstance(MapTestActivity.this).find(new Callback() {
                //					public void onFindFinished(Location location) {
                //						GeoPoint point = new GeoPoint((int) (location.latitude * 1E6),
                //								(int) (location.longitude * 1E6));
                //						Log.d("test", location.latitude + ", " + location.longitude);
                //						mMapController.setCenter(point);
                //						//mMapView.refresh();
                //					}
                //				});
                Log.d( "test", "onClick" );

                Locator.getInstance( getApplicationContext() ).find( new Callback()
                {
                    public void onFindFinished( BDLocation location )
                    {
                        Log.d( "test", "onClick onFindFinished" );
                        GeoPoint point = new GeoPoint( (int) ( location.getLatitude() * 1E6 ), (int) ( location
                            .getLongitude() * 1E6 ) );
                        Log.d( "test", location.getLatitude() + ", " + location.getLongitude() );

                        MyLocationOverlay myLocationOverlay = new MyLocationOverlay( MapTestActivity.this, mMapView );
                        //						LocationData locData = new LocationData();

                        //						locData.latitude = location.getLatitude();
                        //						locData.longitude = location.getLongitude();
                        //						myLocationOverlay.setData(locData);
                        mMapView.getOverlays().add( myLocationOverlay );
                        myLocationOverlay.enableCompass();
                        mMapView.invalidate();

                        mMapController.setCenter( point );
                    }
                } );

                //				Log.d("test", "Tapped");
                //				MyLocationOverlay myLocationOverlay = new MyLocationOverlay(mMapView);
                //				LocationData locData = new LocationData();
                //				myLocationOverlay.setData(locData);
                //				mMapView.getOverlays().add(myLocationOverlay);
                //				myLocationOverlay.enableCompass();
                //				mMapView.refresh();
            }
        } );
    }

    @Override
    protected void onPause()
    {
        super.onPause();
        mBMapManager.stop();
        mBMapManager.destroy();
        mBMapManager = null;
    }

    private void initMapManager()
    {
        mBMapManager = new BMapManager( this );
        mBMapManager.init( Constant.KEY, null );
    }

}
